import React, { Component } from 'react'
import { nanoid } from 'nanoid'
import { connect } from 'react-redux'
import {createAddPersonAction} from '../../redux/actions/persion'
class Person extends Component {
  add = () => {
      const data = {
        id: nanoid(),
        name: this.personName.value,
        age: this.age.value
      }
      this.props.add(data)
      this.personName.value = ''
      this.age.value = ''
  }
  render() {
    return (
      <div>
        <h2>我是Person组件, 上方组件求和为{this.props.count}</h2>
        <input ref={c => this.personName = c}/> <input ref={c => this.age = c}/> <button onClick={this.add}>add</button>
        <ul>
           {
            this.props.personList.map(w => {
              return <li key={w.id}>{w.name}--{w.age}</li>
            })
           }
        </ul>
      </div>
    )
  }
}

export default connect(
  state => ({count: state.he,personList: state.rens}),
  {
    add: createAddPersonAction
  }
)(Person)